﻿/*
创建人：longb
日期：2022-06-24
描述：临时配送提请js
*/
var page = {
    pagetotal: 0,
    pageSize: 20,
    pageNumber: 1,
    ajaxUrl: getRootPath() + "/Ashx/ZsHandler/ZSIssueCollectMaterialHandler.ashx",
    comboxUrl:getRootPath() + "/Ashx/ZsHandler/ZSSendRequestHandler.ashx",//combox下拉-调用后台的URL
    comboxAction:"GetAllTools",//combox下拉-调用后台的具体方法
    //初始化页面
    init: function () {
        page.bind();
        customizedEasyuiPagination('pp'); //定制化分页
    },
    //页面事件绑定
    bind: function () {
        $('#OrderType').combobox({
            data: [{ id: 1, text: '正常' }, { id: 0, text: '临时'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#BigSmall').combobox({
            data: [{ id: 0, text: '否' }, { id: 1, text: '是'}],
            valueField: 'id',
            textField: 'text'
        });
        $('#OrderType').combobox('setValue', 1);
        $('#BigSmall').combobox('setValue', 1);
        $('#ProductCombox').selectCombox({
            search: function () { },
            cancel: function () { },
            selected: function (val, text,record){
                $('#Productid').val(val);
                $('#ProductName').val(record.PRODUCTNAME);
                $('#txtCombox').textbox('setValue',text);
                $('#divComboxTxt').show();
                $('#divComboxSelect').hide();
                $('#divComboxSearch').show();
                $('#divComboxClear').hide();
            }
        });
        //任务单信息
        $("#gdContainerInfo").datagrid({
            singleSelect: true,
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 //{ field: 'ck', title: '选择', checkbox: true },
                 { field: 'ATTRIBUTEVALUE', title: '发动机号', width: 80 },
                 { field: 'MFGORDERNAME', title: '订单号', width: 150 },
		         { field: 'TOGETHERFLAG', title: '任务单号', width: 150 },
		         { field: 'WORKFLOWTYPE', title: '任务类型', width: 70 }, //--
                 {field: 'PRODUCTNAME', title: '件号', width: 100 },
                 { field: 'DESCRIPTION', title: '件号名称', width: 90 },
                 { field: 'WFDESC', title: '工艺名称', width: 100 },
                 { field: 'PLANNEDSTARTDATE', title: '计划开始时间', width: 100, formatter: function (value, row, index) {
                     if (value != null)
                         return dayjs(value).format("YYYY-MM-DD");
                 }
                 },
                 { field: 'PLANNEDFINISHDATE', title: '计划完成时间', width: 100, formatter: function (value, row, index) {
                     if (value != null)
                         return dayjs(value).format("YYYY-MM-DD");
                 }
                 },
                 { field: 'TASKBILLSTATUS', title: '任务单状态', width: 80 },
                 { field: 'WORKFLOWSTEPNAME', title: '阶段', width: 100 }
	           ]], 
            onSelect: function (index, rowData) { 
                page.GetWorkFlowData();
                page.GetTempData("");
                $('#Togetherflag').textbox('setValue',rowData.TOGETHERFLAG);
                $('#ENGINEPARTPLANID').val(rowData.ENGINEPARTPLANID);
             },
        });
        //工序信息
        $('#StepTable').datagrid({
            fitColumns: true,
            singleSelect: true,
            columns: [[
		            { field: 'SPECNO', title: '工序号', width: 50, align: 'center' },
		            { field: 'OPDESC', title: '工序名称', width: 150 }
                ]],
            onSelect: function (rowIndex, rowData) {
                page.GetTempData(rowData.WORKFLOWSTEPID);
               $('#workflowstepid').combobox('setValue', rowData.WORKFLOWSTEPID);
            }
        });
        $('#TempTable').datagrid({
            checkOnSelect: false,
            selectOnCheck: false,
            singleSelect: true,
            columns: [[
                    { field: 'ck', title: '选择', checkbox: true },
		            { field: 'PRODUCTBATCHNO', title: '件号/批次', width: 100 },
                    { field: 'PRODUCTNAME', title: '编码/名称', width: 100 },
		            { field: 'APPLYTYPE', title: '配送任务类型', width: 100,formatter: function (value, row, index) {if(value=="2"){return "临时物料";}else{return "临时工装工具";}} },
		            { field: 'APPLYQTY', title: '申请数量', width: 100, align: 'center' },
                    { field: 'PURPOSE', title: '用途', width: 100, align: 'center' },
                    { field: 'STATUS', title: '状态', width: 100, align: 'center',  },
                    { field: 'FULLNAME', title: '创建人', width: 80, align: 'center',},
                    { field: 'CREATEDATE', title: '创建时间', width: 120, align: 'center', }
                ]],
            onSelect: function (index, row) {
                $("#TempTable").datagrid("uncheckAll").datagrid("checkRow",index);
            },
            onCheckAll:function(rows){
//                $.each(rows,function(index,item){
//                    if(item.STATUS!="待提交"||item.STATUS!="配送完"){ $('#TempTable').datagrid("uncheckRow",index);}
//                });
            },
            onCheck:function(Index,row){
//                if(row.STATUS!="待提交"||item.STATUS!="配送完"){ $('#TempTable').datagrid("uncheckRow",Index);}else{
                var checkRows= $("#TempTable").datagrid("getChecked");
                var selectRow= $("#TempTable").datagrid("getSelected");
                if(checkRows.length>1){
                    $("#TempTable").datagrid("clearSelections");
                }
//                }
            }
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.GetTaskOrderData();
            }
        });
        //绑定分页go按钮
        $('#pageGo').on("click", function () {
            var pageNumber = $(this).parents('tr').find('.pagination-num').val();
            page.pageNumber = pageNumber;
            page.GetTaskOrderData();
        });
        //清除按钮
        $('#btnReset').on("click", function () {
            page.reset(true, true);
            page.pageNumber = 1; page.pagetotal = 0
            $('#pp').pagination('refresh', {
                total: page.pagetotal,
                pageSize: page.pageSize,
                pageNumber: page.pageNumber
            });
        });


    },
    TypeChange:function(){
        //$('#TempForm').form('clear');
        var opt=$("input:radio[name='Type']:checked").val()
        if(opt=="1"){$('#NAME').html("物料编码/名称");page.comboxAction="GetAllProduct";$('#Type'+opt).radiobutton('check');}
        if(opt=="0"){$('#NAME').html("工装工具编码/名称");page.comboxAction="GetAllTools";$('#Type'+opt).radiobutton('check');}
        $('#Productid').val('');
        $('#ProductName').val('');
        $('#txtCombox').textbox('clear')
        $('#Text4').textbox('clear');
        $('#Text5').textbox('clear');
        $('#Text6').textbox('clear');
    },
    //查询
    searchData: function () {
        page.reset(false, true);
        page.pageNumber = 1;
        page.GetTaskOrderData();
    },
    //查询任务单信息
    GetTaskOrderData: function () {
        $('#StepTable').datagrid('loadData', [])
        $('#TempTable').datagrid('loadData', [])
        var frdata = $('#searchForm').serializeArray();
        var data = {HasBom:"1",TEMP:""};
        $.each(frdata, function () {
            data[this.name] = this.value
        });
        var param = {
            action: "GetTaskOrderData",
            type: "",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            data: JSON.stringify([data])
        };
        page.post("1",param, function (res) {
            if (res.Result) {
                //console.info(res);
                page.pagetotal = res.RowCount;
                $('#gdContainerInfo').datagrid('loadData', res.DBTable);
            }
        });
    },
    //查询工序信息
    GetWorkFlowData: function () {
        $('#TempTable').datagrid('loadData', []);
        var CheckedRows = $('#gdContainerInfo').datagrid('getSelected');
        var WorkFlowId = CheckedRows.WORKFLOWID;
        if (WorkFlowId == "") { return; }
        var param = {
            action: "GetWorkFlowData",
            type:"",
            workflowid: WorkFlowId
        };
        page.post("1",param, function (res) {
            if (res.Result) {
                $('#StepTable').datagrid('loadData', res.DBTable)
                $('#workflowstepid').combobox({
                    data: res.DBTable,
                    valueField: 'WORKFLOWSTEPID',
                    textField: 'OPDESC'
                });
            }
        });
    },
    //查询临时配送物料清单
    GetTempData:function(workflowstepid){
      var row = $('#gdContainerInfo').datagrid('getSelected');
        var param = {
            action: "GetTempData",
            workflowstepid: workflowstepid,
            ContainerNo: row.TOGETHERFLAG
        };
        page.post("",param, function (res) {
            $('#TempTable').datagrid('loadData', res.DBTable);
        });
    },
    //新增临时配送物料清单
    AddTemp:function(){
     var frdata = $('#TempForm').serializeArray();
     var data={ApplyType:"2",IssueERPMaterialID:""};
      $.each(frdata, function () {
            data[this.name] = this.value
        });
     if(data.Togetherflag==""){displayMessage("任务单号不能为空",false);return;}
     if(data.WORKFLOWSTEPID==""){displayMessage("工序不能为空",false);return;}
     if(data.Productid==""){displayMessage("物料编码/名称不能为空",false);return;}
//     if(data.ProductBatchNo==""){displayMessage("批次号不能为空",false);return;}
     if(data.FIRSTASSEMBLEQTY==""){displayMessage("申请数量不能为空",false);return;}
       var param = {
            action: "SendRequset",
            ApplyType:"2",
            data: JSON.stringify([data]),
            Aux:JSON.stringify([]),
            Tools:JSON.stringify([])
        };
        page.post("",param, function (res) {
            if(res.Result){
                //$('#TempForm').form('clear');
                var row = $('#gdContainerInfo').datagrid('getSelected');
                var steprow = $('#StepTable').datagrid('getSelected');
                $('#Togetherflag').textbox('setValue',row.TOGETHERFLAG);
                $('#ENGINEPARTPLANID').val(row.ENGINEPARTPLANID);
                if(steprow!=null ){$('#workflowstepid').combobox('setValue', steprow.WORKFLOWSTEPID);}
                $('#Productid').val('')
                $('#txtCombox').textbox('clear');
                $('#Text5').textbox('clear');
                $('#Text6').textbox('clear');
                page.GetTempData(steprow.WORKFLOWSTEPID);
            }
            displayMessage(res.Message,res.Result);
        });
    },
    //删除临时配送
    DeleteTempData:function(){
      var rows = $('#TempTable').datagrid('getChecked');
      var data=[];
      var data1=[];
      $.each(rows, function (index,item) {
        if(item.STATUS=="待提交"){
                if(item.APPLYTYPE=="2"){
                    data.push(item.ISSUEMATERIALTRANSPORTID);
                }else{
                    data1.push(item.ISSUEMATERIALTRANSPORTID);
                }
            }
        });
      if((data.length+data1.length)==0){displayMessage("请勾选临时申请状态【待提交】记录再进行操作");return;}
      var param = {
            action: "DeleteTempData",
            ids:data.join(','),
            tids:data1.join(',')
        };
        page.post("",param, function (res) {
            if(res.Result){page.GetTempData($('#workflowstepid').combobox('getValue'));}
            displayMessage(res.Message,res.Result);
        });
    },
    //提交临时配送
    SubmitTempData:function(){
      var rows = $('#TempTable').datagrid('getChecked');
      var data=[];
      var data1=[];
     $.each(rows, function (index,item) {
            if(item.STATUS=="待提交"){
                if(item.APPLYTYPE=="2"){
                    data.push(item.ISSUEMATERIALTRANSPORTID);
                }else{
                    data1.push(item.ISSUEMATERIALTRANSPORTID);
                }
            }
        });
      if((data.length+data1.length)==0){displayMessage("请勾选临时申请状态【待提交】记录再进行操作");return;}
      var param = {
            action: "SubmitTempData",
            ids:data.join(','),
            tids:data1.join(',')
        };
        page.post("",param, function (res) {
            if(res.Result){page.GetTempData($('#workflowstepid').combobox('getValue'));}
            displayMessage(res.Message,res.Result);
        });
    },
    //临时配送--收料确认
    ReceivedTempData:function(){
     var rows = $('#TempTable').datagrid('getChecked');
      var data=[];
      var data1=[];
     $.each(rows, function (index,item) {
           if(item.STATUS=="配送完"){
                 if(item.APPLYTYPE=="2"){
                    data.push(item.ISSUEMATERIALTRANSPORTID);
                }else{
                    data1.push(item.ISSUEMATERIALTRANSPORTID);
                }
            }
        });
      if((data.length+data1.length)==0){displayMessage("请勾选临时申请状态【配送完】记录再进行操作");return;}
      var param = {
            action: "ReceivedTempData",
            ids:data.join(','),
            tids:data1.join(',')
        };
        page.post("",param, function (res) {
            if(res.Result){page.GetTempData($('#workflowstepid').combobox('getValue'));}
            displayMessage(res.Message,res.Result);
        });
    
    },
    //查询重置
    searchReset: function () {
        $('#searchForm').form('clear');
        page.bind();
    },
    
    //清除方法，重要！！！
    reset: function (iscondition, isresult) {
        clearMessage();
    },
    //post数据
    post: function (t,param, callback) {
        $.ajax({
            type: "Post",
            url: t!=""?page.ajaxUrl:page.comboxUrl,
            data: param,
            cashe: false,
            async: true,
            dataType: "json",
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                callback(data);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    }
}
$(function () {
    page.init();
});